﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.BusinessLogic;
using Module.Core.EntitiesInfo;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Audits;
using Module.Core.LoginSystem.Components.Modules;
using Module.Core.LoginSystem.Components.Roles;
using Module.Core.LoginSystem.Components.Users;
using System.Data;
using Module.EntitiesInfo;

namespace Web.Cms.Advertistment
{
    public partial class AdvertisementPostionManager : AdminUserControl
    {
        private DataTable dt;
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
             dt= AdvertisementPositionsBL.SelectTbl(1,"","");
            if (dt != null && dt.Rows.Count > 0)
            {
                grdAdvertisements.DataSource = dt;
                grdAdvertisements.DataBind();
            }
            RolePermisionInfo _roleInfo = RoleController.GetReadWritePermison(CurrentUser.UserID, CurrentCommand.CommandID);
            if (_roleInfo == null) return;
            if (_roleInfo.Write == 0)
            {

                foreach (GridViewRow row in grdAdvertisements.Rows)
                {
                    Button btnDeleteAdv = (Button)row.FindControl("btnDeleteAdvertise");
                    btnDeleteAdv.Enabled = false;
                    btnDeleteAdv.CssClass = "Icon_DeleteOff";
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void grdAdvertisements_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                RolePermisionInfo _roleInfo = RoleController.GetReadWritePermison(CurrentUser.UserID, CurrentCommand.CommandID);
                if (_roleInfo == null) return;
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

               
              
                Label lblSTT = (Label)e.Row.FindControl("lblSTT");
                lblSTT.Text = (e.Row.DataItemIndex + 1).ToString();
                Label lblSize = (Label)e.Row.FindControl("lblSize");
             
                Label lblCreatedBy = (Label)e.Row.FindControl("lblCreateBy");
                UserInfo _userInfo = UserController.GetUser(CurrentUser.UserID);
                if (_userInfo != null)
                {
                    lblCreatedBy.Text = _userInfo.Username;
                }
                Label lblPosition = (Label)e.Row.FindControl("lblPosition");

               
            }
        }

        protected void grdAdvertisements_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int advId = ConvertUtility.ToInt32(e.CommandArgument);

            try
            {
                switch (e.CommandName)
                {
                    case "deleteadvertise":
                        if (advId == 0) return;
                        //xoa nhom
                        AdvertisementPositionsBL.Delete(advId);
                        // Insert Cores_Audits
                        ModuleInfo _moduleInfo = ModuleController.GetModule(CurrentCommand.CtrlKey);
                        AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.Delete, CurrentCommand.CtrlKey, CurrentUser.UserID, advId
                                                    , "Xóa banner quảng cáo Id:" + advId, 1, DateTime.Now);
                        //end

                        Response.Redirect("/Cms/Default.aspx?ctrlkey=AdvertisementPostionManager");
                        break;

                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
        }

        protected void grdAdvertisements_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdAdvertisements.PageIndex = e.NewPageIndex;
        }
    }
}